"use strict";

const Controller = require("egg").Controller;

/**
 * @controller 标签CRUD
 */
class TagController extends Controller {
  /**
   * @summary 查询所有标签
   * @router get /api/tags
   * @apikey
   */
  async index() {
    const { ctx } = this;
    ctx.body = {
      code: 200,
      msg: "查询所有标签",
      data: await ctx.model.Tag.findAll(),
    };
  }

  /**
   * @summary 创建标签
   * @router post /api/tags
   * @request body TagBody 标签Body
   * @apikey
   */
  async create() {
    const { ctx } = this;
    const tag = ctx.request.body;
    ctx.body = {
      code: 200,
      msg: "创建标签",
      data: await ctx.model.Tag.create(tag),
    };
  }

  /**
   * @summary 更新标签提交
   * @router put /api/tags/{id}
   * @request path number id 标签Id
   * @request body TagBody 标签Body
   * @apikey
   */
  async update() {
    const { ctx } = this;
    const tag = ctx.request.body;
    const id = ctx.params.id;
    const res = await ctx.service.tag.update(tag, id);
    ctx.body = {
      code: 200,
      msg: "更新标签",
      data: "操作数量:" + res,
    };
  }

  /**
   * @summary 刪除标签
   * @router delete /api/tags/{id}
   * @request path number id 标签Id
   * @apikey
   */
  async destroy() {
    const { ctx } = this;
    const tagId = ctx.params.id;
    const res = await ctx.service.tag.destroy(tagId);
    ctx.body = {
      code: 200,
      msg: "删除标签",
      data: "操作数量:" + res,
    };
  }
}

module.exports = TagController;
